package MediaEntity;

import java.awt.*;
import java.io.*;

import javazoom.jl.player.advanced.AdvancedPlayer;
import javazoom.jl.player.advanced.PlaybackEvent;
import javazoom.jl.player.advanced.PlaybackListener;
import org.jaudiotagger.audio.AudioFileIO;
import org.jaudiotagger.audio.exceptions.CannotReadException;
import org.jaudiotagger.audio.exceptions.InvalidAudioFrameException;
import org.jaudiotagger.audio.exceptions.ReadOnlyFileException;
import org.jaudiotagger.audio.mp3.MP3AudioHeader;
import org.jaudiotagger.audio.mp3.MP3File;
import org.jaudiotagger.tag.FieldKey;
import org.jaudiotagger.tag.TagException;
import org.jaudiotagger.tag.id3.AbstractID3v2Frame;
import org.jaudiotagger.tag.id3.AbstractID3v2Tag;
import javazoom.jl.decoder.JavaLayerException;

public class musicStream {
    //可恶，没有计时工具只能自己写了，这是将帧数和播放时间联系在一起的工具。
    static final double FRAME_PER_MILISEC=0.3828125;
    /*一个内部类，聚合，用来包含所有关于jaudiotagger的内容*/
    class MP3info{
        private MP3File mp3File;
        private String album,name,singer;
        private int time;
        private byte[] imageData;//专辑封面数据
        private Image image;//专辑封面图片对象
        MP3info(String filePath){
            try {
                    mp3File = (MP3File)AudioFileIO.read(new File(filePath));
                } catch (CannotReadException | TagException | ReadOnlyFileException | InvalidAudioFrameException |IOException e) {
                    e.printStackTrace();
                }
            AbstractID3v2Tag v2tag= mp3File.getID3v2Tag();
            MP3AudioHeader header=mp3File.getMP3AudioHeader();
            singer = v2tag.getFirst(FieldKey.ARTIST);// 歌手名
            album = v2tag.getFirst(FieldKey.ALBUM);// 專輯名
            name = v2tag.getFirst(FieldKey.TITLE);// 歌名
            time=header.getTrackLength();
            AbstractID3v2Frame frame = (AbstractID3v2Frame) v2tag.getFrame("APIC");
        }

        public String getAlbum() {
            return album;
        }
    }


    private AdvancedPlayer player;
    private int fullSecond;
    private int pausedOnFrame;//这个是帧数，不是秒
    private MP3info mp3info;//MP3Info类包含所有信息
    private String filePath;//当前歌曲对应的文件路径

    /*musicStream的构造函数*/
    public musicStream(String filePath){
        this.filePath=filePath;
        mp3info=new MP3info(filePath);
        fullSecond=mp3info.time;
        regainPlayer();
    }
    //两个play方法分别指定从0开始或者是从特定帧开始，被包装在musicThread的方法中了
    public void play(){
        try {
            this.getPlayer().play();
        } catch (JavaLayerException e) {
            e.printStackTrace();
        }
    }
    public void play(long i){
        try {
            getPlayer().play((int)(i*FRAME_PER_MILISEC),Integer.MAX_VALUE);
        } catch (JavaLayerException e) {
            e.printStackTrace();
        }
    }
    public void close(){
        getPlayer().close();
    }
    public void regainPlayer(){
        File file=new File(filePath);
        FileInputStream fis;
        try {
            fis = new FileInputStream(file);
            BufferedInputStream stream = new BufferedInputStream(fis);
            player=new AdvancedPlayer(stream);
            player.setPlayBackListener(new PlaybackListener() {
                @Override
                public void playbackFinished(PlaybackEvent event) {
                    pausedOnFrame = event.getFrame();
                }
            });
        } catch (FileNotFoundException | JavaLayerException e) {
            e.printStackTrace();
        }

    }

    public AdvancedPlayer getPlayer() {
        return this.player;
    }
    public void setPlayer(AdvancedPlayer p){
        this.player=p;
    }
    public MP3info getMp3info(){
        return this.mp3info;
    }
    public int getPausedOnFrame() {
        return pausedOnFrame;
    }
    public void setPausedOnFrame(int a){
        this.pausedOnFrame=a;
    }
    public String getFilePath(){
        return filePath;
    }
    public int getFullSecond() {
        return fullSecond;
    }



}
